package com.xq.sboot.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Date;

/**
 * 功能说明:第一个Servlet监听器类.
 * <p>创建时间:2018-12-14 11:20</p>
 *
 * @author <a href="https://gitee.com/gkq/microservice">郭凯强</a>
 * @since 1.0.0
 */
@WebListener("firstListener")
public class FirstListener implements HttpSessionListener, ServletRequestListener {
    private final Logger logger = LoggerFactory.getLogger(FirstListener.class);
    private final Marker marker = MarkerFactory.getMarker("FirstListener");

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        logger.debug(marker, "[Listener] 一个session被创建，session's id={}", se.getSession().getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        logger.debug(marker, "[Listener] 一个session即将失效，id={}", se.getSession().getId());
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        logger.debug(marker, "一个请求即将不在application范围，时间为{}", new Date());
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        logger.debug(marker, "一个请求进入application范围，时间为{}", new Date());
    }


}
